﻿//初始化
function swfuploadInfo(uploadDiv)
{
    var swfu = null;
    $(uploadDiv.schedule_div).hide();
    $(uploadDiv.oK_div).hide();
    var settings_object = {
        upload_url: uploadDiv.upload_url,
        flash_url: uploadDiv.flash_url,
        button_placeholder_id: uploadDiv.SWFUploadId,
        button_width: 113,
        button_height: 33,
        file_types: uploadDiv.file_types,
        file_queued_handler: file_queued_handler_function,
        file_queue_error_handler: file_queue_error_function,
        upload_error_handler: upload_error_handler_function,
        upload_success_handler: function (a, b, c) { myCustomUploadSuccessEventHandler(a, b, c, uploadDiv, swfu); },
        upload_progress_handler: function (a, b, c) { upload_progress_handler(a, b, c, uploadDiv, swfu); },
        file_dialog_start_handler: function (file, errorCode, message) { file_dialog_start_handler_function(file, errorCode, message, uploadDiv, swfu) },
        file_queued_handler: function (a) { file_queued_handler_function(a, uploadDiv, swfu) },
        button_image_url: uploadDiv.button_image_url,
        file_size_limit: uploadDiv.file_size_limit,
        file_upload_limit: 0,
        file_queue_limit: 2,
        button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
        button_cursor: SWFUpload.CURSOR.HAND
    };
    swfu = new SWFUpload(settings_object);
}


//上传成功
var myCustomUploadSuccessEventHandler = function (file, server_data, receivedResponse, uploadDiv, swfu)
{
    swfu.setButtonDimensions(113, 33);
    var outString = uploadDiv.callBack(server_data);

    if (uploadDiv.inputName != "#")
    {
        if (outString == null)
        {
            $(uploadDiv.inputName).val(server_data);
        }
        else
        {
            $(uploadDiv.inputName).val(outString);
        }
    }
    $(uploadDiv.oK_div).show();
    $(uploadDiv.schedule_div).hide();

    upload_dialog.close();

};
var upload_dialog = null;
//上传中
var upload_progress_handler = function (file, bytesComplete, bytesTotal, uploadDiv, swfu)
{

    var i = ((bytesComplete / bytesTotal) * 100);

    if (upload_dialog == null && art != null)
    {
        upload_dialog = art.dialog({
            title: '文件上传中',
            id: "upload_dialog",
            icon: "warning",
            fixed: true,
            lock: true,
            content: "上传进度" + i + "%",
            ok: true,
            okVal: "取消上传",
            close: function () { swfu.cancelUpload(); upload_dialog = null; }
        });
    } else
    {
        if (parseInt(i) == 100)
        {
            upload_dialog.content("上传进度" + parseInt(i) + "%");
        } else
        {
            upload_dialog.content("正在保存文件请等待...");
        }
    }


    //显示上传进度
    /* $(uploadDiv.schedule_div).show();
   
    $(uploadDiv.scheduleing_div).width(i);
    */
    //swfu.setButtonDimensions(1, 1);

    // $("#" + uploadDiv.spanSWFUploadButton).hide();
};

//文件选择
var file_queued_handler_function = function (file, uploadDiv, swfu)
{
    //写入当前文件
    uploadDiv.curr_file = file;

    $(uploadDiv.oK_div).hide();
    if (typeof (uploadDiv.param_object) == "object")
    {
        swfu.setPostParams(uploadDiv.param_object);
    } else
    {
        swfu.setPostParams(uploadDiv.param_object());
    }
    swfu.startUpload();
};


//选择文件错误
var file_queue_error_function = function (file, errorCode, message)
{
    if (errorCode == -110)
    {
        alert("超过上传文件大小限制");
    } else if (errorCode == -280)
    {

    } else
    {
        alert("errorCode:" + errorCode + "|message:" + message);
    }
};

//上传过程中发生错误
var upload_error_handler_function = function (file, errorCode, message)
{
    if (errorCode == -110)
    {
        alert("超过上传文件大小限制");
    } else if (errorCode == -280)
    {

    } else
    {
        alert("errorCode:" + errorCode + "|message:" + message);
    }
};

file_dialog_start_handler_function = function (file, errorCode, message, uploadDiv, swfu)
{

    if (typeof (uploadDiv.curr_file) != 'undefined' && uploadDiv.curr_file != null)
    {
        //移除原有文件
        swfu.cancelUpload(uploadDiv.curr_file.id, false);
    }
};

 